import * as Cesium from 'cesium/Cesium'
import MeasurementSettings from "./MeasurementSettings";
import MeasureUnits from "./MeasureUnits";
import PolylineDrawing from "./PolylineDrawing";
import DrawingMode from "./DrawingMode";

var cart2Scratch1$3 = new Cesium.Cartesian2, cart2Scratch2$3 = new Cesium.Cartesian2,
    cart2Scratch3$1 = new Cesium.Cartesian2,
    scratch$1 = new Cesium.Cartesian3;
;


function PolylineMeasurementDrawing(e) {
    (e =  Cesium.defaultValue(e,  Cesium.defaultValue.EMPTY_OBJECT)).polylineOptions
        = MeasurementSettings.getPolylineOptions({ellipsoid: e.ellipsoid}), e.pointOptions = MeasurementSettings.getPointOptions(),
        PolylineDrawing.call(this, e);
    var t = this._scene, i = e.labels;
    this._labelCollection = i, this._label = i.add(MeasurementSettings.getLabelOptions()),
        this._segmentLabels = [], this._selectedUnits = e.units, this._selectedLocale = e.locale, this._previousDistance = 0, this._distance = 0;
    var r = this;
    this._removeEvent = t.preRender.addEventListener(function () {
        r.updateLabel()
    })
}

PolylineMeasurementDrawing.prototype = Object.create(PolylineDrawing.prototype),
    PolylineMeasurementDrawing.prototype.constructor = PolylineMeasurementDrawing,
    Cesium.defineProperties(PolylineMeasurementDrawing.prototype, {
        distance: {
            get: function () {
                return this._distance
            }
        }
    }), PolylineMeasurementDrawing.prototype.updateLabel = function () {
    var e = this._positions;
    if (!(e.length < 2)) {
        var t = this._scene, i = e[0], r =  Cesium.SceneTransforms.wgs84ToWindowCoordinates(t, i, cart2Scratch1$3),
            n =  Cesium.defined(r) ? Cesium.Cartesian2.clone(r, cart2Scratch3$1) :
                Cesium.Cartesian2.fromElements(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, cart2Scratch3$1),
            a = n.y, o = this._segmentLabels;
        o[0].show = 2 < this._polyline.positions.length;
        for (var s = 1; s < e.length; s++) {
            var l =  Cesium.SceneTransforms.wgs84ToWindowCoordinates(t, e[s], cart2Scratch2$3);
            if (Cesium.defined(l)) {
                var c = (n.y - l.y) / (l.x - n.x), u = o[s - 1];
                u.horizontalOrigin = 0 < c ?  Cesium.HorizontalOrigin.LEFT :
                    Cesium.HorizontalOrigin.RIGHT, l.y < a && (a = l.y, i = e[s]), n =  Cesium.Cartesian2.clone(l, n)
            }
        }
        this._mode === DrawingMode.AfterDraw && (this._label.position = i)
    }
}, PolylineMeasurementDrawing.prototype.addPoint = function (e) {
    var t = this._positions;
    if (0 < t.length) {
        this._previousDistance = this._distance;
        var i = this._labelCollection.add(MeasurementSettings.getLabelOptions({
            scale: .8,
            horizontalOrigin:  Cesium.HorizontalOrigin.LEFT,
            verticalOrigin:  Cesium.VerticalOrigin.TOP,
            pixelOffset: new  Cesium.Cartesian2(5, 5)
        })), r = t[t.length - 1];
        i.position =  Cesium.Cartesian3.midpoint(r, e, new  Cesium.Cartesian3),
            i.text = MeasureUnits.distanceToString( Cesium.Cartesian3.distance(r, e), this._selectedUnits.distanceUnits,
                this._selectedLocale), i.show = !0, this._segmentLabels.push(i)
    }
    PolylineDrawing.prototype.addPoint.call(this, e)
}, PolylineMeasurementDrawing.prototype.handleClick = function (e) {
    this._mode === DrawingMode.AfterDraw && this.reset(),
    Cesium.defined(PolylineDrawing.prototype.handleClick.call(this, e)) && (this._label.show = !0, this._polyline.show = !0)
}, PolylineMeasurementDrawing.prototype.handleMouseMove = function (e) {
    var t = PolylineDrawing.prototype.handleMouseMove.call(this, e);
    if ( Cesium.defined(t)) {
        var i = this._positions[this._positions.length - 1], r = t, n =  Cesium.Cartesian3.subtract(r, i, scratch$1),
            a = this._previousDistance +  Cesium.Cartesian3.magnitude(n), o = this._label;
        o.position = r, o.text = MeasureUnits.distanceToString(a, this._selectedUnits.distanceUnits, this._selectedLocale), o.show = !0, this._distance = a
    }
}, PolylineMeasurementDrawing.prototype.reset = function () {
    var e;
    this._label.show = !1, this._label.text = "", this._positions = [], this._polyline.positions = [],
        this._polyline.show = !1, this._previousDistance = 0, this._distance = 0;
    var t = this._points, i = this._pointCollection;
    for (e = 0; e < t.length; e++) i.remove(t[e]);
    t.length = 0;
    var r = this._labelCollection, n = this._segmentLabels;
    for (e = 0; e < n.length; e++) r.remove(n[e]);
    n.length = 0, this._mode = DrawingMode.BeforeDraw, this._lastClickPosition.x = Number.POSITIVE_INFINITY, this._lastClickPosition.y = Number.POSITIVE_INFINITY
}, PolylineMeasurementDrawing.prototype.destroy = function () {
    this._removeEvent();
    var e = this._labelCollection;
    e.remove(this._label);
    for (var t = this._segmentLabels, i = 0; i < t.length; i++) e.remove(t[i]);
    PolylineDrawing.prototype.destroy.call(this)
}
export default PolylineMeasurementDrawing;
